package com.eazy.lkv2.web.controller.activity;

import com.eazy.lkv2.web.entity.Activitys;
import com.eazy.lkv2.web.service.activity.ActivityService;
import com.eazy.lkv2.web.service.hotel.HotelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ywj
 * @date 2016.9.8
 * @desc 活动后台管理
 */
@Controller("activitys")
@RequestMapping("/activity")
public class ActivityController extends BaseController<Activitys, Integer> {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private HotelService hotelService;

    /**
     * 后台活动列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("data", activityService.select());
        return "activity/activity_list";
    }

    /**
     * 跳转到添加活动页面
     */
    @RequestMapping(value = "/toAddActivity", method = RequestMethod.GET)
    public ModelAndView toAddLine() {
        addObject("hotelData", hotelService.select());
        return getSuccess("activity/activity_add");
    }

    /**
     * 新增活动
     * @param activitys
     */
    @RequestMapping(value = "/addActivity", method = RequestMethod.POST)
    public void addActivity(Activitys activitys) {
        String[] title = getParaValues("image");
        String[] success = getParaValues("success");

        if (title == null) {
            renderJsonError("活动图片不能为空");
            return;
        }
        if (success == null) {
            renderJsonError("请先批量上传图片，然后在提交！！！！");
            return;
        }

        String photo_name = title[0];
        photo_name = photo_name.substring(0, photo_name.lastIndexOf("."));
        activitys.setActivityImg(photo_name);
        renderResult(activityService.create(activitys) > 1);
    }

    /**
     * 根据ID删除活动
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deletes/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public String deleteActivity(@PathVariable("ids") String[] ids) {
        for (String id : ids) {
            activityService.removeById(Integer.parseInt(id));
        }
        return "success";
    }

    /**
     * 禁用活动
     * @param id
     * @return
     */
    @RequestMapping(value = "disable/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String disableActivity(@PathVariable("id") String id) {
        Activitys activity = activityService.findById(Integer.parseInt(id));
        if (activity != null && activity.isStatus()) {
            activity.setStatus(false);
        }
        activityService.update(activity);
        return "success";
    }

    /**
     * 启用活动
     * @param id
     * @return
     */
    @RequestMapping(value = "enable/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String enableActivity(@PathVariable("id") String id) {
        Activitys activity = activityService.findById(Integer.parseInt(id));
        if (activity != null && !activity.isStatus()) {
            activity.setStatus(true);
        }
        activityService.update(activity);
        return "success";
    }
}
